package com.yazi.mxz.person.controller.firm;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Paging;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.Goods;
import com.yazi.mxz.common.entity.enums.StoreGoodsSyncFields;
import com.yazi.mxz.common.service.GoodsClassifyService;
import com.yazi.mxz.common.service.GoodsService;
import com.yazi.mxz.person.controller.MyPageLink;
import com.yazi.mxz.person.jackson.filter.GoodsFilter;
import com.yazi.mxz.person.jackson.result.CheckResult;

@Controller("firm-goodsController")
@RequestMapping("/firm/goods")
public class GoodsController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsController.class);

	@Resource
	GoodsService goodsService;
	@Resource
	GoodsClassifyService goodsClassifyService;
	
	@RequestMapping("/list")
	public String list(Goods goods, Searching si, Model model) {
		debug(log, "进入企业商品列表页面");
		
		// 只能查询当前企业的
		Company company = new Company();
		company.setCompanyId(getCompanyId());
		goods.setCompany(company);
		
		// 默认按添加时间倒序
		if (null == si.getSorts()) { 
			List<Sort> sorts = new ArrayList<Sort>();
			sorts.add(new Sort("addTime", true));
			si.setSorts(sorts);
		}
		
		Page<Goods> page = goodsService.list(goods, si);
		PageLink pl = new MyPageLink(page.getPaging());
		
		model.addAttribute("list", page.getList());
		model.addAttribute("pl", pl);
		model.addAttribute("goods", goods);
		model.addAttribute("searchWord", si.getSearchWord());
		return "firm/goods_list";
	}

	@RequestMapping("/addOrEdit")
	public String addOrEdit(String barCode, Model model) {
		debug(log, "进入企业商品添加页面");
		
		model.addAttribute("barCode", barCode);
		model.addAttribute("companyId", getCompanyId());
		return "firm/goods_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public ObjectResult<?> addOrEditDoJson(Goods goods, Long sync) {
		ObjectResult<Long> jsonResult = new ObjectResult<Long>();
		try {
			if (null != goods.getGoodsId()) {
				debug(log, "保存编辑过的企业商品");
				// 防止篡改以下信息
				goods.setAddTime(null);
				goods.setBarCode(null);
				goods.setName(null);
				
				goodsService.editByCompany(getCompany(true), goods, sync);
				jsonResult.setOk();
			} else {
				debug(log, "保存新添加的企业商品");
				
				goodsService.addByCompany(getCompany(true), goods);
				jsonResult.setResult(goods.getGoodsId());
				jsonResult.setOk();
			}
		} catch (MyException e) {
			debug(log, e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "保存企业商品异常");
			jsonResult.setErrorMessage("保存失败!");
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/oneByCode/json")
	@ResponseBody
	public ObjectResult<?> oneByCodeJson(String barCode) {
		debug(log, "根据编码查找企业商品");
		ObjectResult<Goods> jsonResult = new ObjectResult<Goods>();
		jsonResult.setFilter(GoodsFilter.getWithCatergoryAndGoodsClassify());
		try {
			// 根据企业ID和商品编码查询
			Goods goods = new Goods();
			goods.setBarCode(barCode);
			Company company = new Company();
			company.setCompanyId(getCompanyId());
			goods.setCompany(company);
			
			Paging pi = new Searching();
			pi.setPageSize(1);
			
			Page<Goods> page = goodsService.list(goods, pi);
			List<Goods> list = page.getList();
			pi = page.getPaging();
			
			if (pi.getTotalResult() == 1) {
				jsonResult.setResult(list.get(0));
			}
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "根据编码查找企业商品异常");
			jsonResult.setErrorMessage("获取商品信息失败!");
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/syncFields/json")
	@ResponseBody
	public ObjectResult<?> syncFieldsJson() {
		debug(log, "获取可店铺商品可同步字段");
		ObjectResult<StoreGoodsSyncFields[]> jsonResult = new ObjectResult<StoreGoodsSyncFields[]>();
		jsonResult.setResult(StoreGoodsSyncFields.values());
		jsonResult.setOk();
		return jsonResult;
	}
	
	/**
	 * 检查编码是否可用于新增商品<br>
	 * 如果编码对应的商品已经存在,检查结果为失败,并返回已经存在的商品信息,否则表示检查通过.<br>
	 * 本方法目的是供前端表单验证调用(并返回附加信息,即已经存在的商品信息).
	 * @param barCode
	 * @return
	 */
	@RequestMapping("/check4AddDo/json")
	@ResponseBody
	public CheckResult<?> check4AddDoJson(String barCode) {
		debug(log, "检查商品编码是可否可以使用");
		CheckResult<Goods> jsonResult = new CheckResult<Goods>();
		jsonResult.setFilter(GoodsFilter.getWithCatergory());
		try {
			// 根据企业ID和商品编码查询
			Goods goods = new Goods();
			goods.setBarCode(barCode);
			Company company = new Company();
			company.setCompanyId(getCompanyId());
			goods.setCompany(company);
			
			Paging pi = new Searching();
			pi.setPageSize(1);
			
			Page<Goods> page = goodsService.list(goods, pi);
			if (page.getPaging().getTotalResult() == 1) {
				jsonResult.setResult(page.getList().get(0));
				jsonResult.setError("编码已经存在");
			} else {
				jsonResult.setOk("");
			}
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "根据编码查找企业商品异常");
			jsonResult.setError("编码检查失败!");
			jsonResult.setErrorMessage("编码检查失败!");
		}
		
		return jsonResult;
	}

}
